import os, sys, pygame, random, array, 游戏模式
import 箭头,  边界, 结束, 菜单
from pygame.locals import *


from 加载程序 import load_image
import 玩家, 地图, 车辆, 相机, 轨道


TRAFFIC_COUNT = 45
CENTER_W = -1
CENTER_H = -1

def main():
    clock = pygame.time.Clock()
    running = True
    font = pygame.font.Font(None, 24)
    car = 玩家.Player()
    cam = 相机.Camera()
    target = 游戏模式.Finish()
    bound_alert = 边界.Alert()
    time_alert = 结束.Alert()
    info = 菜单.Alert()
    pointer = 箭头.Tracker(int(CENTER_W * 2), int(CENTER_H * 2))

    map_s     = pygame.sprite.Group()
    player_s  = pygame.sprite.Group()
    traffic_s = pygame.sprite.Group()
    tracks_s  = pygame.sprite.Group()
    target_s  = pygame.sprite.Group()
    pointer_s = pygame.sprite.Group()
    timer_alert_s = pygame.sprite.Group()
    bound_alert_s = pygame.sprite.Group()
    menu_alert_s = pygame.sprite.Group()

    # 生成瓷砖
    for tile_num in range (0, len(地图.map_tile)):
        地图.map_files.append(load_image(地图.map_tile[tile_num], False))
    for x in range (0, 10):
        for y in range (0, 10):
            map_s.add(地图.Map(地图.map_1[x][y], x * 1000, y * 1000, 地图.map_1_rot[x][y]))

#负荷跟踪
    轨道.initialize()
#加载完成
    target_s.add(target)
#加载方向
    pointer_s.add(pointer)
#负荷警报
    timer_alert_s.add(time_alert)
    bound_alert_s.add(bound_alert)
    menu_alert_s.add(info)
#负载流量
    车辆.initialize(CENTER_W, CENTER_H)
    for count in range(0, TRAFFIC_COUNT):
        traffic_s.add(车辆.Traffic())

    player_s.add(car)

    cam.set_pos(car.x, car.y)

    while running:

# 检查菜单/重置
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if keys[K_m]:
                    if (info.visibility == True):
                        info.visibility = False
                    else:
                        info.visibility = True
                if (keys[K_p]):
                    car.reset()
                    target.reset()
                if (keys[K_q]):
                    pygame.quit()
                    sys.exit(0)

            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False
                break



#检查键输入。
        keys = pygame.key.get_pressed()
        if (target.timeleft > 0):
            if keys[K_LEFT]:
                car.steerleft()
            if keys[K_RIGHT]:
                car.steerright()
            if keys[K_UP]:
                car.accelerate()
            else:
                car.soften()
            if keys[K_DOWN]:
                car.deaccelerate()

        cam.set_pos(car.x, car.y)


#显示文本数据。
        text_fps = font.render('FPS: ' + str(int(clock.get_fps())), 1, (224, 16, 16))
        textpos_fps = text_fps.get_rect(centery=25, centerx=60)

        text_score = font.render('Score: ' + str(target.score), 1, (224, 16, 16))
        textpos_score = text_fps.get_rect(centery=45, centerx=60)

        text_timer = font.render('Timer: ' + str(int((target.timeleft / 60)/60)) + ":" + str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16))
        textpos_timer = text_fps.get_rect(centery=65, centerx=60)

#渲染场景。
        screen.blit(background, (0,0))


        map_s.update(cam.x, cam.y)
        map_s.draw(screen)
        
#条件呈现/影响
        car.grass(screen.get_at(((int(CENTER_W-5), int(CENTER_H-5)))).g)
        if (car.tracks):
            tracks_s.add(轨道.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir))


        tracks_s.update(cam.x, cam.y)
        tracks_s.draw(screen)
        
        player_s.update(cam.x, cam.y)
        player_s.draw(screen)

        traffic_s.update(cam.x, cam.y)
        traffic_s.draw(screen)

        target_s.update(cam.x, cam.y)
        target_s.draw(screen)

        pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x, target.y)
        pointer_s.draw(screen)

#条件呈现。
        if (边界.breaking(car.x + CENTER_W, car.y + CENTER_H) == True):
            bound_alert_s.update()
            bound_alert_s.draw(screen)
        if (target.timeleft == 0):
            timer_alert_s.draw(screen)
            car.speed = 0
            text_score = font.render('Final Score: ' + str(target.score), 1, (224, 16, 16))
            textpos_score = text_fps.get_rect(centery=CENTER_H+56, centerx=CENTER_W-20)
        if (info.visibility == True):
            menu_alert_s.draw(screen)
            

        screen.blit(text_fps, textpos_fps)
        screen.blit(text_score, textpos_score)
        screen.blit(text_timer, textpos_timer)
        pygame.display.flip()

#检查碰撞
        if pygame.sprite.spritecollide(car, traffic_s, False):
            car.impact()
            target.car_crash()

        if pygame.sprite.spritecollide(car, target_s, True):
            target.claim_flag()
            target.generate_finish()
            target_s.add(target)
            
        clock.tick(64)
        

#初始化
pygame.init()

screen = pygame.display.set_mode((pygame.display.Info().current_w,
                                  pygame.display.Info().current_h),
                                  pygame.FULLSCREEN)


pygame.display.set_caption('赛车竞赛.')
pygame.mouse.set_visible(False)
font = pygame.font.Font(None, 24)

CENTER_W =  int(pygame.display.Info().current_w /2)
CENTER_H =  int(pygame.display.Info().current_h /2)

#新建背景表面
background = pygame.Surface(screen.get_size())
background = background.convert_alpha()
background.fill((26, 26, 26))

main()

pygame.quit()
sys.exit(0)